﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace CSP.Receivers
{
    class CentralConfigFileFeatureReceiver : ConfigFileFeatureReceiver
    {

        protected string _defaultMembershipProvider = "AspNetMembershipProvider";
        protected string _connectionStringName = "AspNetMembershipSQL";
        protected string _connectionStringContent = "Data Source=(local);Initial Catalog=AspNetDb_CSP;Trusted_Connection=yes;";

        /// <summary>
        /// Gets the connection string from the machine's registry
        /// </summary>
        protected string ConnectionStringContent
        {
            get
            {
                try
                {
                    return CSP.Core.RegistryAccess.GetStringProperty(Registry.LocalMachine, CSP.Core.Constants.CspRegistryKey, "FBAConnectionString");
                }
                catch (Exception ex)
                {
                    return _connectionStringContent;
                }
            }
        }


        /// <summary>
        /// Holds the ModificationEntry objects that must be applied as section nodes.
        /// </summary>
        protected override ModificationEntry[] GetSectionEntries() 
        {
            List<ModificationEntry> modifications = new List<ModificationEntry>();
            //Add Section nodes
            modifications.Add(new ModificationEntry("appSettings", "configuration","appSettings"));
            modifications.Add(new ModificationEntry("connectionStrings", "configuration","connectionStrings"));
            modifications.Add(new ModificationEntry("membership", "configuration/system.web","membership"));
            modifications.Add(new ModificationEntry("providers", "configuration/system.web/membership","providers"));
            modifications.Add(new ModificationEntry("roleManager", "configuration/system.web","roleManager"));
            modifications.Add(new ModificationEntry("providers", "configuration/system.web/roleManager","providers"));
            return modifications.ToArray();

        }

        /// <summary>
        /// Holds the ModificationEntry objects that must be applied as child nodes.
        /// </summary>
        protected override ModificationEntry[] GetChildNodeEntries()
        {
            List<ModificationEntry> modifications = new List<ModificationEntry>();
            modifications.Add(
                new ModificationEntry(
                    string.Format("add[@name='{0}']", _connectionStringName),
                    "configuration/connectionStrings",
                    string.Format(@"<add name=""{0}"" connectionString=""{1}"" />", _connectionStringName, ConnectionStringContent))
                    );

            modifications.Add(
                new ModificationEntry(
                    string.Format("add[@name='{0}']", _defaultMembershipProvider),
                    "configuration/system.web/membership/providers",
                    string.Format(@"<add name=""{0}"" connectionStringName=""{1}"" enablePasswordRetrieval=""false"" enablePasswordReset=""true"" requiresQuestionAndAnswer=""false"" applicationName=""/"" requiresUniqueEmail=""false"" passwordFormat=""Hashed"" maxInvalidPasswordAttempts=""5"" minRequiredPasswordLength=""1"" minRequiredNonalphanumericCharacters=""0"" passwordAttemptWindow=""10"" passwordStrengthRegularExpression="""" type=""System.Web.Security.SqlMembershipProvider,System.Web,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"" />", _defaultMembershipProvider, _connectionStringName))
                    );

            return modifications.ToArray();
        }

        /// <summary>
        /// Holds the ModificationEntry objects that must be applied as attributes.
        /// </summary>
        protected override ModificationEntry[] GetAttributeEntries()
        {

            List<ModificationEntry> modifications = new List<ModificationEntry>();
            return modifications.ToArray();
        }

    }
}
